class TagsController < ApplicationController
  include AuthenticatedSystemBrowse
  
  def index
    @page_title = "全部标签"
    @tags = Tag.related_tags(:order => "updated_at desc")
  end
  
  def show
    if params[:id].nil?
      redirect_to :controller => 'tags', :action => 'index'
    elsif params[:type].nil? || params[:type_id].nil?
      @related_tags = Array.new
      
      @posts = Post.find_tagged_with(:any => params[:id], :page => params[:page], :order=>'replied_at DESC')
      
      @related_tags += Tag.related_tags(:limit => 20, :order => "updated_at desc", :posts_ids => @posts.collect{|n| n.id}.join(','))
      @related_tags.reject! {|t| t.name == params[:id] }
      @page_title = params[:id] + " - 标签"
    elsif params[:type] == 'community' || params[:type] == 'group' || params[:type] == 'site' || params[:type] == 'district'
      @posts = Post.find_tagged_with_by_type({:any => params[:id], :page => params[:page], :order=>'replied_at DESC'}, params[:type], params[:type_id])
      @related_tags = Array.new
      if params[:type] == 'community'
        @community = Community.find(params[:type_id])
        @related_tags += Tag.related_tags(:limit => 20, :order => "updated_at desc", :type => "common", :geog => "community", :geog_id => @community.id, :posts_ids => @posts.collect{|n| n.id}.join(','))
        @related_tags.reject! {|t| t.name == params[:id] }
      end
      if params[:type] == 'district'
        @district = District.find(params[:type_id])
        @related_tags += Tag.related_tags(:limit => 20, :order => "updated_at desc", :type => "common", :geog => "district", :geog_id => @district.id, :posts_ids => @posts.collect{|n| n.id}.join(','))
        @related_tags.reject! {|t| t.name == params[:id] }
      end
      if params[:type] == 'group'
        @group = Group.find(params[:type_id])
        @related_tags += Tag.related_tags(:limit => 20, :order => "updated_at desc", :type => "group", :which => @group.id, :posts_ids => @posts.collect{|n| n.id}.join(','))
        @related_tags.reject! {|t| t.name == params[:id] }
      end
      if params[:type] == 'site'
        @site = Site.find(params[:type_id])
        @related_tags += Tag.related_tags(:limit => 20, :order => "updated_at desc", :type => "site", :which => @site.id, :posts_ids => @posts.collect{|n| n.id}.join(','))
        @related_tags.reject! {|t| t.name == params[:id] }
      end
      @page_title = params[:id] + " - 标签"
    else
      @posts = Post.find_tagged_with(:any => params[:id], :page => params[:page], :order=>'replied_at DESC')
      @page_title = params[:id] + " - 标签"
    end
  end
  
  def user
    @user = User.find_by_login(params[:u])
    
    @posts = Post.find_tagged_with_by_user({:any => params[:id], :page => params[:page], :order=>'replied_at DESC'}, @user.id)
  end
  
end
